package com.example.careycai.notepad;




import android.content.ContentValues;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import java.util.Calendar;
import java.util.Date;


import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



public class Inforactivity extends AppCompatActivity {

    private MyDatabaseHelper dbHelper;
    private TextView title_note,time_note;
    private TelEdit te;
    private EditText place_text;
    private Button update_note;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.information);

        Intent intent=getIntent();
        final String time1=intent.getStringExtra("time");


        title_note=(TextView)findViewById(R.id.title_note);
        time_note=(TextView)findViewById(R.id.time_note);
        place_text=(EditText)findViewById(R.id.editText8);
        te=(TelEdit)findViewById(R.id.EditText01);
        update_note=(Button)findViewById(R.id.update_note);
        dbHelper=new MyDatabaseHelper(this,"Notepad.db",null,1);
        SQLiteDatabase db=dbHelper.getWritableDatabase();
        String sql1="select id,title,place,content,time from information  where time LIKE '%"+time1+"%'";
        Cursor cursor=db.rawQuery(sql1,null);
        if(cursor.moveToNext()){
            do{
                String title=cursor.getString(cursor.getColumnIndex("title"));
                String place=cursor.getString(cursor.getColumnIndex("place"));
                String content=cursor.getString(cursor.getColumnIndex("content"));
                String time=cursor.getString(cursor.getColumnIndex("time"));
                title_note.setText(title);
                place_text.setText(place);
                te.setText(content);
                time_note.setText(time);
            }while(cursor.moveToNext());
        }
        cursor.close();

        update_note.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                String dt;
                Date cal = Calendar.getInstance().getTime();
                dt = cal.toLocaleString();
                ContentValues values=new ContentValues();
                values.put("title",title_note.getText().toString());
                values.put("place",place_text.getText().toString());
                values.put("content",te.getText().toString());
                values.put("time", dt);
                db.update("information", values, "time=?", new String[]{time1});
                values.clear();
                Toast.makeText(Inforactivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                Intent intent1=new Intent(Inforactivity.this,MainActivity.class);
                startActivity(intent1);
            }
        });

    }
}
